package mipo.top.pet.controller;

import mipo.top.admin.domain.Employee;
import mipo.top.admin.service.IEmployeeService;
import mipo.top.base.service.ILoginService;
import mipo.top.base.util.JsonResult;
import mipo.top.base.util.LoginContext;
import mipo.top.base.util.PageUtil;
import mipo.top.pet.Service.IPetService;
import mipo.top.pet.domain.Pet;
import mipo.top.pet.query.PetQuery;
import mipo.top.users.domain.Logininfo;
import mipo.top.users.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping
    public List<Pet> sad(){
        return petService.findall();
    }

    @PutMapping
    public JsonResult su(@RequestBody Pet pet, HttpServletRequest request){

        try{
            Logininfo loginUser = LoginContext.getLoginUser(request);
            Employee employee=employeeService.findBylogininfoId(loginUser.getId());
            pet.setShopId(employee.getShopId());
            pet.setUserId(employee.getId());
            if(pet.getId()==null){
                petService.save(pet);
            }else{
                petService.update(pet);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("错误数据");
        }
    }

    @DeleteMapping("/{id}")
    public void adw(@PathVariable("id") Long id){
        petService.remove(id);
    }

    @PostMapping
    public PageUtil<Pet> asp(@RequestBody PetQuery petQuery){
        return petService.queryData(petQuery);
    }
    @PatchMapping
    private void bacthDel(@RequestBody List<Long> ids){
        petService.patchremove(ids);
    }

    @PostMapping("/offsale")
    public JsonResult offsale(@RequestBody List<Long> ids){
        try{
            petService.offsale(ids);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }
    @PostMapping("/onsale")
    public JsonResult onsale(@RequestBody List<Long> ids){
        try{
            petService.onsale(ids);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id") Long id){
        return petService.getById(id);
    }
}
